#!/usr/bin/env jq
# compile-config-0.02-parse_schema -- Parses variables in deepdive.schema
##
include "constants";
include "util";

.deepdive_ as $deepdive

# parse variable schema to define some names (mainly to find categorical cardinality)
| .deepdive_.schema.variables_ = [
    $deepdive.schema.variables | to_entries[]
    | .key as $relationName | (.value | to_entries) as $columns
    | ([$columns[] | .value | ascii_downcase] | unique) as $variableTypes
    | ([$columns[] | .key]) as $columnNames
        | if $variableTypes | length == 1 then .
        else error("deepdive.schema.variables.\($relationName) has \(length) columns declared as variable. Every relation in the schema must have more than one column declared as a variable of the same type")
        end
        |  $columns[0].key as $columnName | $variableTypes[0] as $variableType |
        { variableName         : $relationName
        , variablesTable       : $relationName
        , variablesIdsTable    : "\(deepdivePrefixForVariablesIdsTable)\($relationName)"
        , variablesWithIdsTable: "\(deepdivePrefixForVariablesWithIdsTable)\($relationName)"
        , variablesKeyColumns  : [ [ $deepdive.schema.relations[$relationName].columns | to_entries[]? ]
            | [.[] | select([.value.annotations[]? | .name == "key"] | any)] as $keyColumns
            | if $keyColumns | length == 0
            then .  # all user columns are considered keys if none were annotated as @key
            else $keyColumns  # or use only the key columns to find distinct variables
            end
            | .[].key
            ]
        , variablesParitionColumns  : [ [ $deepdive.schema.relations[$relationName].columns | to_entries[]? ]
            | [.[] | select([.value.annotations[]? | .name == "partition"] | any)]
            | .[].key
            ]
        , variableType : ($variableType | trimWhitespace | ascii_downcase
            | if in({boolean: 1, categorical: 1}) then .
            else error("deepdive.schema.variables.\($relationName).\($columnName) has an unrecognized type: \($variableType | @json)")
            end)
        }
        | .variablesCategoryColumns = if .variableType == "boolean" then []          else $columnNames                                                      end
        | .variablesCategoriesTable = if .variableType == "boolean" then null        else "\(deepdivePrefixForVariablesCategoriesTable)\($relationName)"    end
        | .variablesLabelColumn     = if .variableType == "boolean" then $columnName else deepdiveVariableLabelColumn                                       end
]

# create a map to make it easy to access a variable by its name
| .deepdive_.schema.variables_byName = (.deepdive_.schema.variables_ | map({key: .variableName, value: .}) | from_entries)
